問題描述
如何在測試工具中實例化 asp.net 代碼隱藏類? (How can I instantiate an asp.net codebehind class in a test harness?)
I'm working on a web site written using asp.net WebForms. I'd like two wrap test cases around some of the more interesting subroutines. How can I instantiate the class that comes from the .aspx file in my test project so I can manipulate it under nUnit?
Edit: What I really want to do is test the utility methods and event methods that are in the code‑behind. I don't want to post to the page and read the response. I want to unit test the Methods, not the Page.
‑‑‑‑‑
參考解法
方法 1:
UPDATE Make sure you have setup your project to be an ASP.NET web project, not an asp.net web site. You can then mark your page class with the appropriate NUnit attributes and test the output dll for your project with NUnit.
Here is a Microsoft article that explains unit testing in asp.net: http://msdn.microsoft.com/en‑us/library/ms404696(VS.80).aspx
方法 2:
You can instantiate it just like any other type:
YourPage page = new YourPage();
Now getting the lifecycle to run will be quite another matter.
(by Sean McMillan、jjxtra、Andrew Hare)